<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  xmlns:rc="http://www.fmi.org/RecipesCatalog" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
	<xsl:output method="xml" indent="yes" />

	<xsl:variable name="nsPrefix">rc</xsl:variable>
	<xsl:variable name="ns">http://www.fmi.org/RecipesCatalog</xsl:variable>
	
	<!--  Root  -->
	<xsl:template match="RecipesCatalog"  priority="0.6">
		<xsl:element name="rdf:RDF">
			<rdf:Description>
				<xsl:attribute name="rdf:about">
					<xsl:value-of select="concat('#',name())" />
				</xsl:attribute>
				<xsl:apply-templates select="*">
					<xsl:with-param name="rdfAboutURI" select="name()"/>
				</xsl:apply-templates>
			</rdf:Description>
		</xsl:element>
	</xsl:template>
	<!-- Elements that have children  -->
	<xsl:template match="*[*]">
		<xsl:param name="rdfAboutURI"/>
		<xsl:variable name="newRdfAboutURI">
			<xsl:value-of select="concat($rdfAboutURI,'/', name())"/>
			<!-- Check if parent has more than one element with name of the current.
				If so give the current element unique rdf:about value -->
			<xsl:if test="count(../*[name()=name(current())]) > 1">
				<xsl:text>_</xsl:text>
				<xsl:number/>
			</xsl:if>
		</xsl:variable>
		<xsl:element name="{$nsPrefix}:{name()}" namespace="{$ns}">
			<rdf:Description>
				<xsl:attribute name="rdf:about">
					<xsl:value-of select="concat('#', $newRdfAboutURI)" />
				</xsl:attribute>
				<xsl:apply-templates select="@* | node()">
					<xsl:with-param name="rdfAboutURI" select="$newRdfAboutURI" />
				</xsl:apply-templates>
			</rdf:Description>
		</xsl:element>
	</xsl:template>
	<!-- Elements that have no children  -->
	<xsl:template match="*[not(*)]">
		<xsl:element name="{$nsPrefix}:{name()}" namespace="{$ns}">
			<xsl:value-of select="."/>
		</xsl:element>
		<xsl:apply-templates select="@*" />
	</xsl:template>
	<!-- Apply order to the preparation steps -->
	<xsl:template match="PreparationSteps" priority="0.6">
		<xsl:element name="{$nsPrefix}:{name()}" namespace="{$ns}">
			<rdf:Seq>
				<xsl:for-each select="Step">
					<rdf:li><xsl:value-of select="."/></rdf:li>
   				</xsl:for-each>
			</rdf:Seq>
		</xsl:element>
	</xsl:template>
	<!-- 
	Element with attribute id has to be referenced from elements with href attribute
	so generate rdf:about according to element's id attribute
	-->
	<xsl:template match="*[@id]" priority="0.5">
		<xsl:param name="rdfAboutURI"/>
		<xsl:element name="{$nsPrefix}:{name()}" namespace="{$ns}">
			<rdf:Description>
				<xsl:attribute name="rdf:nodeID">
					<xsl:value-of select="@id" />
				</xsl:attribute>
				<xsl:apply-templates select="@* | node()">
					<xsl:with-param name="rdfAboutURI" select="concat(@id,'/')" />
				</xsl:apply-templates>
			</rdf:Description>
		</xsl:element>
	</xsl:template>
	<!-- All attributes -->
	<xsl:template match="@*">
		<xsl:element name="{$nsPrefix}:{name()}" namespace="{$ns}">
			<xsl:value-of select="."/>
		</xsl:element>
	</xsl:template>
	<!-- Special handling of href attribute -->
	<xsl:template match="@href">
		<xsl:element name="{$nsPrefix}:{name()}" namespace="{$ns}">
			<xsl:attribute name="rdf:nodeID">
				<xsl:value-of select="."/>
			</xsl:attribute>
		</xsl:element>
	</xsl:template>
	
</xsl:stylesheet>